import { reactive, toRefs } from "vue";
// 设置传统表格中空的字段value为 -
const setNullTableKey = (list) => {
  if (!list || !list.length) {
    return [];
  }
  return list.map((item) => {
    Object.keys(item).map((k) => {
      if (item[k] === null || item[k] === "") {
        item[k] = "-";
      }
    });
    return item;
  });
};
// 获取表格数据
export const useTableList = () => {
  const state = reactive({
    tableData: [],
    total: 0,
    loading: false,
  });
  const setTableList = async (callBack, params = {}) => {
    try {
      state.loading = true;
      const data = await callBack(params);
      state.loading = false;
      state.total = data.total || 0;
      state.tableData = data.contents || data.list || [];
      if (state.tableData.length) {
        state.tableData = setNullTableKey(state.tableData);
      }
    } catch (e) {
      state.loading = false;
    }
  };
  return { ...toRefs(state), setTableList };
};
